package com.supersit.greensproject.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.supersit.greensproject.R;
import com.supersit.greensproject.resources.API;
import com.supersit.greensproject.utils.AppManager;
import com.supersit.greensproject.utils.DesUtil;
import com.supersit.greensproject.utils.HttpXUtils;
import com.supersit.greensproject.utils.MyCallBack;
import com.supersit.greensproject.utils.SPUtils;
import com.supersit.greensproject.utils.T;
import com.supersit.greensproject.view.MyTitleBar;
import com.supersit.greensproject.view.ShowDialog;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

/**
 * 修改密码
 * Created by cong on 2016/7/14.
 */
@ContentView(R.layout.activity_udpate_password)
public class UpdatePasswordActivity extends BaseActivity {
    @ViewInject(R.id.et_old_pwd)
    private EditText etOldPwd;
    @ViewInject(R.id.et_new_pwd)
    private EditText etNewPwd;
    @ViewInject(R.id.et_confirm_pwd)
    private EditText edConfirmPwd;
    @ViewInject(R.id.btn_update_submit)
    private Button btnSubmit;
    private Context context;

    private MyTitleBar myTitleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        context = this;
        setTitleBar();
        bindListener();
    }

    private void setTitleBar() {
        myTitleBar = new MyTitleBar(context, findViewById(R.id.update_password_title));
        myTitleBar.setTitleText(getResources().getString(R.string.update_pwd));
        myTitleBar.setChildVisibility(false, myTitleBar.getmImgRight());
    }

    private void bindListener() {
        myTitleBar.setImg1Listener(onClickListener);
        btnSubmit.setOnClickListener(onClickListener);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.title_bar_left:
                    finish();
                    break;
                case R.id.btn_update_submit:
                    if (checkPassword()) {
                        uploadPwd();
                    }
                    break;
            }
        }
    };

    private boolean checkPassword() {
        String OldPassword = etOldPwd.getText().toString().trim();
        String newPassword = etNewPwd.getText().toString().trim();
        String confirmPwd = edConfirmPwd.getText().toString().trim();
        String pwd = (String) SPUtils.get(context, "password", "");
        if (TextUtils.isEmpty(OldPassword)) {
            T.showShort(context, getString(R.string.old_pwd_null));
            return false;
        }
        if (TextUtils.isEmpty(newPassword)) {
            T.showShort(context, getString(R.string.new_pwd_null));
            return false;
        }
        if (TextUtils.isEmpty(confirmPwd)) {
            T.showShort(context, getString(R.string.confirmPwd_pwd_null));
            return false;
        }
        if (!pwd.equals(OldPassword)) {
            T.showShort(context, getString(R.string.old_pwd_error));
            return false;
        }
        if (!newPassword.equals(confirmPwd)) {
            T.showShort(context, getString(R.string.new_pwd_inconformity));
            return false;
        }
        return true;
    }

    private void uploadPwd() {
        int userId = (int) SPUtils.get(context, "userId", -1);
        String key = (String) SPUtils.get(context, "key", "12345678");
        String url = API.updatePwd;
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        try {
            map.put("pwd", DesUtil.encrypt(etNewPwd.getText().toString().trim(), key));
        } catch (Exception e) {
            e.printStackTrace();
        }
        final ShowDialog showDialog = new ShowDialog(context);
        showDialog.showLoadingDialog(getString(R.string.pwd_updateing));
        HttpXUtils.Post(url, map, new MyCallBack<String>() {
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                showDialog.dismissLoadDialog();
                T.showShort(context, R.string.service_error);
            }

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                showDialog.dismissLoadDialog();
                if ("1".equals(result)) {
                    T.showShort(context, R.string.update_pwd_success);
                    AppManager.getAppManager().finishAllActivity();
                    startActivity(new Intent(context, LoginActivity.class));
                    finish();
                } else if ("0".equals(result)) {
                    T.showShort(context, R.string.update_pwd_fail);
                }
            }
        });
    }
}
